package lyx.tank.stages
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	
	import lyx.game.GameEngine;
	import lyx.game.element.GameElement;
	import lyx.game.locator.ElementLocator;
	import lyx.game.stage.GameStage;
	import lyx.tank.element.TankBase;
	import lyx.tank.gamelevel.LevelInfo;
	
	public class GameingStage extends GameStage
	{
		private var _game_view:Bitmap = new Bitmap();
		private var _game_view_data_cache:BitmapData;
		
		private var _level_info:LevelInfo;
		
		public function GameingStage()
		{
				
		}
		
		public function init(info:Object):void
		{
			_level_info = info.level_info;
		}
		
		
		override protected function initGameStageView():void
		{	
			graphics.beginFill(0x909090);
			graphics.drawRect(0,0,480,480);
			graphics.endFill();
			
			_game_view = new Bitmap(new BitmapData(480,480,true,0));
			addChild(_game_view);
			_game_view_data_cache = new BitmapData(480,480,true,0)
		}
		
		override protected function initGameStageEvent():void
		{
			GameEngine.getInstance().startEngine(_level_info);
			//start the render engine of GameingStage
			addEventListener(Event.ENTER_FRAME,viewRenderHandler);
		}
		
		override protected function destoryGameStageEvent():void
		{
			//stop the render engine of GameingStage
			removeEventListener(Event.ENTER_FRAME,viewRenderHandler);
		}
		
		protected function viewRenderHandler(event:Event):void
		{
			_game_view_data_cache.fillRect(_game_view_data_cache.rect,0);
			for each(var view_element:GameElement in ElementLocator.GAME_ELEMENTS)
			{
				view_element.render(_game_view_data_cache);
			}
			_game_view.bitmapData.copyPixels(_game_view_data_cache,_game_view_data_cache.rect,new Point());
		}
	}
}